package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.Admin;
import com.example.wjh.entity.Coach;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Admin")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;

    //按用户名搜索
    @GetMapping("/info/{username}")
    public Result getAdminInfoByUsername(@PathVariable("username") String username) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Admin admin= adminMapper.selectOne(queryWrapper);
        if (admin != null) {
            return Result.success(admin);
        } else {
            return Result.error("Coach not found for username: " + username);
        }
    }

    /**
     * 查询
     * @return
     */
    @GetMapping()
    public Result getAdmins(){  return Result.success(adminMapper.selectList(null));}

    /**
     * 添加球员
     * @param Admin
     * @return
     */
    @PostMapping()
    public Result createAdmin(@RequestBody Admin Admin) {
        adminMapper.insert(Admin);
        return Result.success();
    }

    /**
     * 更新
     * @param id
     * @param admin
     * @return
     */
    @PutMapping("/{id}")
    public Result updateAdmin(@PathVariable("id") int id, @RequestBody Admin admin) {
        Admin existingAdmin = adminMapper.selectById(id);
        if (existingAdmin != null) {
            admin.setId(id);
            adminMapper.updateById(admin);
            return Result.success();
        } else {
            return null;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delAdmin(@PathVariable("id") int id) {
        System.out.println(id);
        adminMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result delAdminBatch(@RequestBody List<Long> ids) {
        adminMapper.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 模糊查询
     * @param name
     * @return
     */
    @GetMapping("/AdminByName/{name}")
    public Result findByName(@PathVariable String name) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        List<Admin> Admins = adminMapper.selectList(queryWrapper);
        return Result.success(Admins);
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public Result getAdminList(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) {
        Page<Admin> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id"); // 根据id字段降序排序
        page.addOrder(OrderItem.desc("id")); // 添加降序排序条件
        IPage ipage = adminMapper.selectPage(page,null);
        return Result.success(ipage);
    }

}
